from BaseController import BaseController
from MTurkUtility import ACCESS_KEY
from MTurkUtility import SECRET_KEY
from Utility import ViewName
from Utility import UserRole

class HomeController(BaseController):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        u = self.GetCurrentUser()

        template_values = {
            'RESULT': ACCESS_KEY,
            'SECRET_KEY': SECRET_KEY
            }
        if u is None:
            return self.Render(ViewName.LOGIN, template_values)

        if u is not None and u.Role == UserRole.User and u.HITs is not None:
            hits = []
            for currentHit in u.HITs:
                createdHit = {"duration":currentHit.getDurationText()}
                createdHit['title'] = currentHit.Title
                createdHit['reward'] = currentHit.Amount
                createdHit['campaign'] = currentHit.Ref_Campaign.CampaignName
                createdHit['expiration'] = currentHit.getExpirationText()
                createdHit['client'] = currentHit.Ref_Client.ClientName
                hits.append(createdHit)
            if len(hits) > 0:
                template_values["hits"] = hits
            
        
        self.Render(ViewName.HOME, template_values)
    
    def post(self):
        self.get(self)
        
